package com.linkoog.devtools.code_generator.wizard.generate.database.template;

import com.intellij.openapi.project.Project;
import com.linkoog.devtools.code_generator.template.render.TemplateRender;
import com.linkoog.devtools.code_generator.wizard.generate.database.template.panel.BottomPanelBuilder;
import com.linkoog.devtools.code_generator.wizard.generate.database.template.panel.LeftPanelBuilder;
import com.linkoog.devtools.code_generator.wizard.generate.database.template.panel.RightPanelBuilder;
import com.linkoog.devtools.code_generator.KeyConstants;
import com.linkoog.devtools.code_generator.wizard.GenerateWizardContext;
import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.search.GlobalSearchScope;
import com.linkoog.devtools.intellij.wizard.WizardContext;
import com.linkoog.devtools.intellij.wizard.WizardStep;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class TemplateSelectConfigStep extends WizardStep {

    private JPanel myMainPanel;


    private LeftPanelBuilder leftPanelBuilder;
    private RightPanelBuilder rightPanelBuilder;
    private BottomPanelBuilder bottomPanelBuilder;


    public TemplateSelectConfigStep(Project project, WizardContext wizardContext) {
        super(project, wizardContext);
        initContextPanel();

        // initBottomPanel();
    }


    private void initContextPanel() {
        leftPanelBuilder = new LeftPanelBuilder();
        rightPanelBuilder = new RightPanelBuilder();

        // 模板树--点击事件
        leftPanelBuilder.setChickConsumer(templateNodeData -> {
            rightPanelBuilder.resetData(templateNodeData);
        });


        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.3F);
        //splitter.setResizeEnabled(false);
        splitter.setFirstComponent(leftPanelBuilder.getMyMainPanel());
        splitter.setSecondComponent(rightPanelBuilder.getMyMainPanel());
        this.myMainPanel.add(splitter, BorderLayout.CENTER);
    }


    private void initBottomPanel() {
        this.bottomPanelBuilder = new BottomPanelBuilder();
        this.myMainPanel.add(bottomPanelBuilder.getMyMainPanel(), BorderLayout.SOUTH);
    }

    @Override
    public void onStepPrepare() {
        GenerateWizardContext generateWizardContext = (GenerateWizardContext) getWizardContext();
        VirtualFile selectedPackageVF = generateWizardContext.getSelectedPackageVF();
        if ("controller".equals(selectedPackageVF.getName())){
            selectedPackageVF = selectedPackageVF.getParent();
        }

        generateWizardContext.setSelectedPackageVF(selectedPackageVF);

        rightPanelBuilder.setPackageTextField(generateWizardContext.getSelectedPackage());
        rightPanelBuilder.setModule(generateWizardContext.getModule());

        String author = System.getProperty("user.name");
        rightPanelBuilder.setAuthorTextField(author);

        rightPanelBuilder.setSelectPackageCallback(psiPackage -> {
            PsiDirectory[] psiDirectories = psiPackage.getDirectories(GlobalSearchScope.moduleRuntimeScope(generateWizardContext.getModule(),false));
            if (psiDirectories.length == 1){
                rightPanelBuilder.setPackageTextField(psiPackage.getQualifiedName());
                generateWizardContext.setBasePsiDirectory(psiDirectories[0]);
            }
        });
    }

    @Override
    public boolean validate() throws ConfigurationException {
        String author = rightPanelBuilder.getAuthorTextField();
        if (StringUtils.isBlank(author)) {
            throw new ConfigurationException("请输入用户名！", "错误");
        }

        TemplateNodeData templateNodeData = rightPanelBuilder.getSelectedTemplateNodeData();
        if (templateNodeData == null) {
            throw new ConfigurationException("请选择模板！", "错误");
        }
        return true;
    }

    @Override
    public void onStepOK() {
        GenerateWizardContext context = (GenerateWizardContext) getWizardContext();
        TemplateNodeData templateNodeData = rightPanelBuilder.getSelectedTemplateNodeData();
        context.setTemplateNodeData(templateNodeData);

        String author = rightPanelBuilder.getAuthorTextField();
        context.put(KeyConstants.AUTHOR, author);

        final Project project = getProject();
        context.put(KeyConstants.BASE_PACKAGE, context.getSelectedPackage());

        TemplateRender templateRender = TemplateRender.getInstance(project);
        templateRender.generateCode(context.getBasePsiDirectory(), context.getTableInfoList(), context.getTemplateNodeData(), new HashMap<>(context.getDataMap()));
    }


    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    @Override
    public JComponent getComponent() {
        return myMainPanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(5, 5));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
